//
//  FPSqliteManager.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/22.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPSqliteManager.h"
#define kUserDB @"UserDB"
#define kUserTable @"UserMessage"
#define kCollectMovieTabel @"CollectMovie"
#define kMovieTable @"Movie"
#define kWebTable @"web"
#define kCollectionWeb @"collectionWeb"
#define kCinemaTable @"cinema"
#define kCollectionCinema @"collectionCinema"
@implementation FPSqliteManager


static FMDatabase *db = nil;
+ (NSString *)filePath:(NSString *)dbName
{
    NSString *name  = [NSString stringWithFormat:@"%@.db",dbName];
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject] stringByAppendingPathComponent:name];
}
+ (void)createUserTable
{
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sqlCreateTable = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement, userName text not null , userPwd text not null, userPhone text, userImage blob)",kUserTable];
        BOOL b = [db executeUpdate:sqlCreateTable];
        if (b)
        {
            NSLog(@"建表成功");
            NSLog(@"%@",[self filePath:kUserDB]);
        }
        else
        {
            NSLog(@"建表失败%@",[db lastErrorMessage]);
        }
        [db close];
    }
    db = nil;
}


+ (void)insertUserModel:(FPUserModel *)userModel
{
    if (!userModel ) {
        NSLog(@"插入model为空");
        return;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    
    if ([db open])
    {
        
        NSData *imageData = UIImageJPEGRepresentation(userModel.userImage, 1.0);
        NSString *sql = [NSString stringWithFormat:@"insert into %@(userName, userPwd, userPhone, userImage) values(?,?,?,?)",kUserTable];
        BOOL b = [db executeUpdate:sql ,userModel.userName, userModel.userPwd, userModel.userPhone, imageData];
        if (b) {
            NSLog(@"插入成功");
        }
        else
        {
            NSLog(@"插入失败%@",[db lastErrorMessage]);
        }
        [db close];
    }
    db = nil;
}
+ (NSMutableArray *)selectAllUserName
{
    NSMutableArray *array = [NSMutableArray array];
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@",kUserTable];
        FMResultSet *rs = [db executeQuery:sql];
        
        while ([rs next]) {
            NSString *userName = [rs stringForColumn:@"userName"];
            
            [array addObject:userName];
        }
        [db close];
    }
    db = nil;
    return array;
}
+ (NSMutableArray *)selectAllUserPWD
{
    NSMutableArray *array = [NSMutableArray array];
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@",kUserTable];
        FMResultSet *rs = [db executeQuery:sql];
        
        while ([rs next]) {
            NSString *userName = [rs stringForColumn:@"userPwd"];
            
            [array addObject:userName];
        }
        [db close];
    }
    db = nil;
    return array;
}


+ (void)createMovieTable
{
    
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sqlCreateTable = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement,titleCN text,titleEN text,movieImage text,actor text,ratingFinal text,directorName text,rDay text,rMonth text,rYear text,time text,movieID text)   ",kMovieTable];
        BOOL b = [db executeUpdate:sqlCreateTable];
        if (b)
        {
            NSLog(@"建表成功");
            NSLog(@"%@",[self filePath:kUserDB]);
        }
        else
        {
            NSLog(@"建表失败%@",[db lastErrorMessage]);
            
        }
        [db close];
    }
    db = nil;
}
+ (void)insertMovieModel:(FPmainTopViewModel *)movieModel
{
    if (!movieModel) {
        NSLog(@"插入model为空");
        return;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open])
    {
        NSString *sql = [NSString stringWithFormat:@"insert into %@(titleCN, titleEN, movieImage, actor, ratingFinal, directorName, rDay, rMonth, rYear, time, movieID) values(?,?,?,?,?,?,?,?,?,?,?)",kMovieTable];
        BOOL b = [db executeUpdate:sql , movieModel.titleCn, movieModel.titleEn,  movieModel.imgstr, movieModel.actor1 , movieModel.ratingFinal, movieModel.directorName, movieModel.rDay, movieModel.rMonth, movieModel.rYear, movieModel.time, movieModel.movieID];
        if (b) {
            NSLog(@"插入成功");
        }
        else
        {
            NSLog(@"插入失败");
            NSLog(@"失败%@",[db lastErrorMessage]);
            
        }
        [db close];
        
        
        
    }
    db = nil;
}
+ (NSMutableArray *)selectMovieTableAllMovieID
{
    
    NSMutableArray *array = [NSMutableArray array];
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@",kMovieTable];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString *movieID = [rs stringForColumn:@"movieID"];
            [array addObject:movieID];
        }
        [db close];
        
    }
    db = nil;
    return array;
    
}
+ (FPmainTopViewModel *)selectModelWithMovieID:(NSString *)movieID
{
    if (!movieID) {
        NSLog(@"筛选失败");
        return nil;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    FPmainTopViewModel *model = [[FPmainTopViewModel alloc]init];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ where movieID = ?",kMovieTable];
        FMResultSet *rs = [db executeQuery:sql,movieID];
        while ([rs next]) {
            model.movieID = [rs stringForColumn:@"movieID"];
            model.titleCn = [rs stringForColumn:@"titleCN"];
            model.titleEn = [rs stringForColumn:@"titleEN"];
            model.imgstr = [rs stringForColumn:@"movieImage"];
            model.actor1 = [rs stringForColumn:@"actor"];
            model.ratingFinal = [rs stringForColumn:@"ratingFinal"];
            model.directorName = [rs stringForColumn:@"directorName"];
            model.rDay = [rs stringForColumn:@"rDay"];
            model.rMonth = [rs stringForColumn:@"rMonth"];
            model.rYear = [rs stringForColumn:@"rYear"];
            model.time = [rs stringForColumn:@"time"];
        }
        [db close];
    }
    db = nil;
    return model;
}



+ (void)createCollectMovieTable
{
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open])
    {
        NSString *sqlCreateTable = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement, userName text, movieID text)",kCollectMovieTabel];
        BOOL b = [db executeUpdate:sqlCreateTable];
        if (b) {
            NSLog(@"建表成功");
            NSLog(@"%@",[self filePath:kUserDB]);
        }
        else
        {
            NSLog(@"建表失败");
        }
        [db close];
    }
    db = nil;
}

+ (void)insertCollectTable:(FPCollectMovieModel *)model
{
    if (!model) {
        NSLog(@"插入的MODEL为空");
        return;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into %@(userName,movieID) values(? , ?)",kCollectMovieTabel];
        BOOL b = [db executeUpdate:sql,model.userName, model.movieID];
        if (b) {
            NSLog(@"插入成功");
        }
        else
        {
            NSLog(@"插入失败");
        }
        [db close];
    }
    db = nil;
}
+ (void)deleteDataWithMovieID:(NSString *)movieID
{
    if (!movieID) {
        NSLog(@"movieID为空");
        return;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@ where movieID = ?",kCollectMovieTabel];
        BOOL b = [db executeUpdate:sql,movieID];
        
        if (!b) {
            NSLog(@"错误");
        }
        else
        {
            NSLog(@"成功");
        }
        [db close];
        
    }
    db = nil;
    
}


+ (NSMutableArray *)selectMovieWithUserName:(NSString *)userName
{
    if (!userName) {
        NSLog(@"名字为空");
        return nil;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    NSMutableArray *array = [NSMutableArray array];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ where userName = ?",kCollectMovieTabel];
        FMResultSet *rs = [db executeQuery:sql, userName];
        while ([rs next]) {
            NSString *str = [rs stringForColumn:@"movieID"];
            
            [array addObject:str];
            
            
        }
        [db close];
    }
    db = nil;
    return array;
    
}


+ (void)createWebTable
{
    
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open])
    {
        NSString *sqlCreateTable = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement, prevuePic text, title text, title2 text)",kWebTable];
        BOOL b = [db executeUpdate:sqlCreateTable];
        if (b) {
            NSLog(@"建表成功");
            NSLog(@"%@",[self filePath:kUserDB]);
        }
        else
        {
            NSLog(@"建表失败");
        }
    }
    
}

+ (void)insertWebTable:(FPIntroModel *)model
{
    if (!model) {
        NSLog(@"插入的MODEL为空");
        return;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        
        NSString *sql = [NSString stringWithFormat:@"insert into %@(prevuePic, title, title2) values(?, ?, ?)",kWebTable];
        BOOL b = [db executeUpdate:sql, model.image, model.title ,model.title2];
        if (b) {
            NSLog(@"插入成功");
        }
        else
        {
            NSLog(@"插入失败");
        }
        [db close];
    }
    db = nil;
    
}

+ (NSMutableArray *)selectAllWebTitle
{
    NSMutableArray *array = [NSMutableArray array];
    if (!db)
    {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    
    if ([db open])
    {
        NSString *sql = [NSString stringWithFormat:@"select * from %@",kWebTable];
        FMResultSet *rs = [db executeQuery:sql];
        
        while ([rs next])
        {
            NSString *title = [rs stringForColumn:@"title"];
            
            [array addObject:title];
        }
        [db close];
    }
    db = nil;
    return array;
}

+ (FPIntroModel *)selectAllWebModelWithTitle:(NSString *)title
{
    
    if (!db)
    {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    FPIntroModel *model = [[FPIntroModel alloc]init];
    
    if ([db open])
    {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ where title = ?",kWebTable];
        FMResultSet *rs = [db executeQuery:sql,title];
        while ([rs next])
        {
            model.image = [rs stringForColumn:@"prevuePic"];
            model.title = [rs stringForColumn:@"title"];
            model.title2 = [rs stringForColumn:@"title2"];
        }
        [db close];
    }
    db = nil;
    return model;
}

+ (void)createWebCollectTable
{
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open])
    {
        NSString *sqlCreateTable = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement, userName text, title text)",kCollectionWeb];
        BOOL b = [db executeUpdate:sqlCreateTable];
        if (b) {
            NSLog(@"建表成功");
            NSLog(@"%@",[self filePath:kUserDB]);
        }
        else
        {
            NSLog(@"建表失败");
        }
    }
    
}
+ (void)insertWebCollectTable:(FPCollectionWebModel *)model
{
    if (!model) {
        NSLog(@"插入的MODEL为空");
        return;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into %@(userName,title) values(? , ?)",kCollectionWeb];
        BOOL b = [db executeUpdate:sql,model.userName, model.title];
        if (b) {
            NSLog(@"插入成功");
        }
        else
        {
            NSLog(@"插入失败");
        }
        [db close];
    }
    db = nil;
    
}


+ (NSMutableArray *)selectWebCollectTitleWithUserName:(NSString *)userName
{
    if (!userName) {
        NSLog(@"名字为空");
        return nil;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    NSMutableArray *array = [NSMutableArray array];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ where userName = ?",kCollectionWeb];
        FMResultSet *rs = [db executeQuery:sql, userName];
        while ([rs next]) {
            NSString *str = [rs stringForColumn:@"title"];
            NSLog(@"%@",str);
            [array addObject:str];
        }
        [db close];
    }
    db = nil;
    return array;
}

+ (void)deleteWebModelWithTitle:(NSString *)title
{
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sqlCreateTable = [NSString stringWithFormat:@"delete from %@ where title = ?",kCollectionWeb];
        BOOL b = [db executeUpdate:sqlCreateTable,title];
        if (b) {
            NSLog(@"删除成功");
            }
        else{
            NSLog(@"删除失败");
        }
        [db close];
    }
    db = nil;
}









/*===========================================================================*/
+ (void)createCinemaTable
{
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sqlCreateTable = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement, name text,address text)",kCinemaTable];
        BOOL b = [db executeUpdate:sqlCreateTable];
        if (b) {
            NSLog(@"建表成功");
            NSLog(@"%@",[self filePath:kUserDB]);
        }
        else{
            NSLog(@"建表失败%@",[db lastErrorMessage]);
        }
        [db close];
    }
    db = nil;
}


+ (void)insertCinemaTable:(FPCinemaModel *)model
{
    if (!model) {
        NSLog(@"插入Model为空");
        return;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into %@(name,address) values(?,?)",kCinemaTable];
        BOOL b = [db executeUpdate:sql , model.name, model.address];
        if (b) {
            NSLog(@"插入成功");
        }else
        {
            
            NSLog(@"插入失败");
            NSLog(@"插入失败%@",[db lastErrorMessage]);
        }
        [db close];
    }
    db = nil;
}

+ (NSMutableArray *)selectAllCinemaName
{
    NSMutableArray *array = [NSMutableArray array];
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@",kCinemaTable];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString *name = [rs stringForColumn:@"name"];
            [array addObject:name];
        }
        [db close];
    }
    db = nil;
    return array;
}

+ (FPCinemaModel *)selectCinemaModelWithCinemaName:(NSString *)cinemaName
{
    if (!cinemaName) {
        NSLog(@"筛选失败");
        return nil;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    FPCinemaModel *model = [[FPCinemaModel alloc] init];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ where name = ?",kCinemaTable];
        FMResultSet *rs = [db executeQuery:sql,cinemaName];
        while ([rs next]) {
            model.name = [rs stringForColumn:@"name"];
            model.address    = [rs stringForColumn:@"address"];
        }
        [db close];
    }
    db = nil;
    return model;
}


+ (void)createCinemaCollectTable
{
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sqlCreateTable = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement,name text,userName text)",kCollectionCinema];
        BOOL b = [db executeUpdate:sqlCreateTable];
        if (b) {
            NSLog(@"建表成功");
            NSLog(@"%@",[self filePath:kUserDB]);
        }
        else{
            NSLog(@"建表失败%@",[db lastErrorMessage]);
        }
        [db close];
    }
    db = nil;
}

+ (void)insertCinemaCollectTable:(FPCollectCinemaModel *)model
{
    if (!model) {
        NSLog(@"插入Model为空");
        return;
    }
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into %@(name,userName) values(?,?)",kCollectionCinema];
        BOOL b = [db executeUpdate:sql , model.name, model.userName];
        if (b) {
            NSLog(@"插入成功");
        }else
        {
            
            NSLog(@"插入失败");
            NSLog(@"插入失败%@",[db lastErrorMessage]);
        }
        [db close];
    }
    db = nil;
}

+ (NSMutableArray *)selectCinemaCollectWithUserName:(NSString *)userName
{
    if (!userName) {
        NSLog(@"名字为空");
        return nil;
    }
    NSMutableArray *array = [NSMutableArray array];
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ where userName = ?",kCollectionCinema];
        FMResultSet *rs = [db executeQuery:sql, userName];
        while ([rs next]) {
            NSString *name = [rs stringForColumn:@"name"];
            NSLog(@"---%@",name);
            [array addObject:name];
        }
        [db close];
    }
    db = nil;
    return array;
}


+ (void)deleteCinemaModelWithCinemaName:(NSString *)cinemaName
{
    if (!db) {
        db = [FMDatabase databaseWithPath:[self filePath:kUserDB]];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@ where name = ?",kCollectionCinema];
        BOOL b = [db executeUpdate:sql,cinemaName];
        if (b) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
        [db close];
    }
    db = nil;
}





















@end

